#!/bin/bash -x
# Script for performance testing.
# Run several commands and have the output redirected to a file.

# File names: Each executable name and the test number (for different inputs)
# makes up the filename for the first type of performance test output.
# This file has "support_value elapsed_time number_of_processes" on each line.
# Second type of file records the number of closed itemsets for the given 
# support. In summary, cpMpiReduce1.txt is a first type of file that has 
# results from cpMpiReduce executable, processing input 1. Likewise 
# cpSimpleReduce2.txt will have results from cpSimpleReduce executable working
# on input 2.
# Test output files are placed inside the "tests" directory.

# First make the stack size unlimited. This is needed in serializing the trees.
ulimit -s unlimited
# Delete any previous test results.
rm tests/*
# FIXME: Increase up to 17.
# Decrease to 3 for testing.
maxProcs=17

############################# TEST 1 ##########################################
transactions1=125155
# DONE: Change executable names so that they end with MPI implementation:
# i.e.: cpMpiReduce.openmpi or cpSimpleReduce.mvapich etc.
# FIXME: Lower down to 45k.
minTrans1=145000

minSup=$((transactions1 - transactions1 / 10))
numberOfProcs=1

# Run with number of processes from 3 to 16.
while [ $numberOfProcs -lt $maxProcs ]; do
    minSup=$((transactions1 - transactions1 / 10))
    while [ $minSup -gt $minTrans1 ]; do
        echo -n `mpirun -np $numberOfProcs ./cpMpiReduce.openmpi -r -i data.nitems_0.1.patlen_80.ntrans_250.ascii.125155 -t 125155 -d 2 -s $minSup` >> tests/cpMpiReduce1.txt
        echo " $numberOfProcs" >> tests/cpMpiReduce1.txt
        echo -n "$numberOfProcs $minSup " >> tests/correctness.txt
        echo `wc -l output.txt` >> tests/correctness.txt
        minSup=$((minSup - transactions1 / 10))
    done
    numberOfProcs=$((numberOfProcs + 1))
done

# Run with 2 processors, using cpSimpleReduce. Also print the number of 
# itemsets.
minSup=$((transactions1 - transactions1 / 10))
percentage=90

while [ $minSup -gt $minTrans1 ]; do
    echo -n `mpirun -np 2 ./cpSimpleReduce.openmpi -r -i data.nitems_0.1.patlen_80.ntrans_250.ascii.125155 -t 125155 -d 2 -s $minSup` >> tests/cpSimpleReduce1.txt
    echo " 2" >> tests/cpSimpleReduce1.txt
    echo -n `wc -l output.txt` >> tests/numberOfItemsets1.txt
    echo " $percentage" >> tests/numberOfItemsets1.txt
    percentage=$((percentage - 10))
    minSup=$((minSup - transactions1 / 10))
done

# Run the serial version of the algorithm.
minSup=$((transactions1 - transactions1 / 10))

while [ $minSup -gt $minTrans1 ]; do
    echo `./cpSerial.openmpi -r -i data.nitems_0.1.patlen_80.ntrans_250.ascii.125155 -t 125155 -d 2 -s $minSup` >> tests/cpSerial1.txt
    echo -n "S $minSup " >> tests/correctness.txt
    echo `wc -l output.txt` >> tests/correctness.txt
    minSup=$((minSup - transactions1 / 10))
done

############################# TEST 2 ##########################################
echo  >> tests/correctness.txt
echo "TEST2" >> tests/correctness.txt
echo  >> tests/correctness.txt
# Run with the retail data.
transactions2=88161
# FIXME: Change below to 30.
# Increase to 40k for testing.
minTrans2=30

# Write a function for custom minSupport creation.
function setSupport () {
    # If the minSup value is greater than 10000, subtract 10% (~8000), else 
    # if it is greater than 2500, subtract 2% (~1700). Else subtract 0.20% 
    # (roughly ~176). End when minSup is smaller than 30.
    if [ $minSup -gt 10000 ]; then
        minSup=$((minSup - transactions2 / 10))
    elif [ $minSup -gt 2500 ]; then
        minSup=$((minSup - transactions2 / 25))
    elif [ $minSup -gt 1200 ]; then
        minSup=$((minSup - transactions2 / 100))
    elif [ $minSup -gt 400 ]; then
        minSup=$((minSup - transactions2 / 500))
    elif [ $minSup -gt 150 ]; then
        minSup=$((minSup - transactions2 / 1000))
    else
        minSup=$((minSup - transactions2 / 8000))
    fi
}

# Run with 2 processors, cpSimpleReduce. Also print the number of itemsets.
# FIXME
# minSup=$((transactions2 * 3 / 5))
minSup=0

while [ $minSup -gt $minTrans2 ]; do
    echo -n `mpirun -np 2 ./cpSimpleReduce.openmpi -r -i input.txt.88161 -t 88161 -d 1 -s $minSup` >> tests/cpSimpleReduce2.txt
    echo " 2" >> tests/cpSimpleReduce2.txt
    echo -n `wc -l output.txt` >> tests/numberOfItemsets2.txt
    echo -n " " >> tests/numberOfItemsets2.txt
    echo `python -c "print ($minSup / $transactions2.0)"` >> tests/numberOfItemsets2.txt
    setSupport
done

# Run with 1 to maxProcs number of processes. Use cpMpiReduce executable.
numberOfProcs=8
while [ $numberOfProcs -lt $maxProcs ]; do
    minSup=$((transactions2 * 3 / 5))
    while [ $minSup -gt $minTrans2 ]; do
        echo -n `mpirun -np $numberOfProcs ./cpMpiReduce.openmpi -r -i input.txt.88161 -t 88161 -d 1 -s $minSup` >> tests/cpMpiReduce2.txt
        echo " $numberOfProcs" >> tests/cpMpiReduce2.txt
        echo -n "$numberOfProcs $minSup " >> tests/correctness.txt
        echo `wc -l output.txt` >> tests/correctness.txt
        setSupport
    done
    if [ $numberOfProcs -lt 4 ]; then
        numberOfProcs=$((numberOfProcs + 1))
    else
        numberOfProcs=$((numberOfProcs + 4))
    fi
done

# Run the serial version.
minSup=$((transactions2 * 3 / 5))

while [ $minSup -gt $minTrans2 ]; do
    echo `./cpSerial.openmpi -r -i input.txt.88161 -t 88161 -d 1 -s $minSup` >> tests/cpSerial2.txt
    echo -n "S $minSup " >> tests/correctness.txt
    echo `wc -l output.txt` >> tests/correctness.txt
    setSupport
done
